package com.token.iot.bratterycharging.boss.service;

import java.awt.image.BufferedImage;
import java.io.File;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.outstanding.framework.base.OutstandingService;
import com.outstanding.framework.base.TransactionLogger;
import com.outstanding.framework.bootstrap.SystemPropertyConfigure;
import com.outstanding.framework.core.PendingException;
import com.outstanding.framework.core.ReturnCode;
import com.outstanding.framework.core.SysCode;
import com.token.base.device.api.DeviceStatus;
import com.token.base.device.api.OnlineState;
import com.token.iot.bratterycharging.boss.dao.DeviceDAO;
import com.token.iot.bratterycharging.boss.qrcode.QrCodeUtils;
import com.token.iot.bratterycharging.boss.web.DeviceDTO;
import com.token.iot.bratterycharging.boss.web.ReqPageDto;
import com.token.iot.bratterycharging.boss.web.ResCode;
import com.token.iot.bratterycharging.gateway.api.BusinessFacade;
import com.token.iot.bratterycharging.gateway.api.Device;
import com.token.iot.bratterycharging.gateway.api.Port;

@Service
public class DeviceService extends OutstandingService {
	@Autowired
	private DeviceDAO deviceDao;

	// 注意这里是@Reference而不是@Autowired,如果provider有版本号，则必须带上版本号。否则会出现forbid的情况
	@Reference(version = "1.0.0")
	private BusinessFacade businessFacade;

	private static String logfile = SystemPropertyConfigure.getProperty("log.filepath");
	private static String qrULR = SystemPropertyConfigure.getProperty("qr.url");
	private static String qrfilePath = SystemPropertyConfigure.getProperty("qr.filepath");

	/**
	 * 添加设备,生成二维码
	 */
	public DeviceDTO addDevice(DeviceDTO dto, Long operid) throws PendingException {
		File logFile = new File(logfile);
		debug("imei=" + dto.getImei());
		String url = qrULR + dto.getImei();
		dto.setCreateBy(operid);

		BufferedImage image = QrCodeUtils.createQRCodeWithLogo(url, logFile);
		QrCodeUtils.pressText(dto.getImei(), image);
		String str = QrCodeUtils.getImageBase64String(image);
		dto.setQrContent(url);
		dto.setQrCode(str);
		File f = new File(qrfilePath + dto.getImei() + ".png");
		dto.setQrFileName(dto.getImei() + ".png");
		QrCodeUtils.convertBase64StringToImage(str, f);
		deviceDao.insertforAgent(dto);
		return dto;

	}

	/**
	 * 代理商将设备签收
	 */
	public DeviceDTO checkinDevice(DeviceDTO dto) throws PendingException {
		String agentId = dto.getAgentId();
		Long id = dto.getId();
		deviceDao.updateforcheckin(id, agentId);
		return dto;

	}

	/**
	 * 设备列表
	 */
	public PageInfo listByAgentId(int pageNo, int pageSize, String agentId) throws PendingException {
		// 注入插件参数
		PageHelper.startPage(pageNo, pageSize);
		List<DeviceDTO> list = deviceDao.listByAgentId(agentId);
		PageInfo pageInfo = new PageInfo(list);
		return pageInfo;
	}
	
	
	/**
	 * 设备列表
	 */
	public PageInfo query(int pageNo, int pageSize,ReqPageDto dto) throws PendingException {
		
		// 注入插件参数
		PageHelper.startPage(pageNo, pageSize);
		
		List<DeviceDTO> list = deviceDao.query(dto);
		PageInfo pageInfo = new PageInfo(list);
		return pageInfo;
	}

	/**
	 * 设备列表
	 */
	public DeviceDTO selectOne(String imei) throws PendingException {
		DeviceDTO dto = deviceDao.selectOne(imei);

		try {
			List<Port> data = businessFacade.getPortInfo(imei);
			dto.setPorts(data);
		} catch (Exception e) {
			error(ResCode.DeviceInfError.getMessage(),e);
		}
		return dto;
	}

	/**
	 * 设备列表
	 */
	public PageInfo listByMerchantId(int pageNo, int pageSize, String merchantId) throws PendingException {
		// 注入插件参数
		PageHelper.startPage(pageNo, pageSize);
		List<DeviceDTO> list = deviceDao.listByMerchantId(merchantId);
		PageInfo pageInfo = new PageInfo(list);
		return pageInfo;
	}

	public PageInfo listall(int pageNo, int pageSize) {
		// 注入插件参数
		PageHelper.startPage(pageNo, pageSize);
		List<DeviceDTO> list = deviceDao.listall();
		PageInfo pageInfo = new PageInfo(list);
		return pageInfo;
	}

	/**
	 * 代理商将设备分配给商家
	 */
	public DeviceDTO distributionDevice(DeviceDTO dto) throws PendingException {
		String agentId = dto.getAgentId();
		Long merchantId = dto.getMerchantId();
		Long id = dto.getId();
		deviceDao.updatefordistribution(id, agentId, merchantId);
		return dto;

	}

	public ReturnCode onOnline(Device device) throws PendingException {
		if (device == null || StringUtils.isBlank(device.getImei())) {
			throw new PendingException(ResCode.QDATEEror);
		}
		deviceDao.updateforDeviceOnLine(device.getImei());
		return SysCode.SUCCESS;
	}

	public ReturnCode onOffline(Device device) throws PendingException {
		if (device == null || StringUtils.isBlank(device.getImei())) {
			throw new PendingException(ResCode.QDATEEror);
		}
		deviceDao.updateforDeviceOffLine(device.getImei());
		return SysCode.SUCCESS;
	}

}
